package com.ThreadDemo;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import com.lg.entity.User;
import com.system.util.MyThreadPoolTaskExecutor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyThreadPoolTaskExecutorDemo {

	
	public static void main(String[] args) throws Exception {
		
		MyThreadPoolTaskExecutor taskExecutor = new MyThreadPoolTaskExecutor();
		// 核心线程数：线程池创建时候初始化的线程数
		taskExecutor.setCorePoolSize(5);
		// 最大线程数：线程池最大的线程数，只有在缓冲队列满了之后才会申请超过核心线程数的线程
		taskExecutor.setMaxPoolSize(10);
		// 缓冲队列：用来缓冲执行任务的队列
		taskExecutor.setQueueCapacity(1000);
		// 允许线程的空闲时间60秒：当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
		taskExecutor.setKeepAliveSeconds(60);
		// 线程池名的前缀：设置好了之后可以方便我们定位处理任务所在的线程池
		taskExecutor.setThreadNamePrefix("asyncTask-");
		// 等待时长
		taskExecutor.setAwaitTerminationSeconds(60);
		// 设置策略
		taskExecutor.afterPropertiesSet();
		
		Runnable r1 = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				log.info("=============r1");
			}
		};
		
		log.info("======taskExecutor.execute前");
		taskExecutor.execute(r1);
		log.info("======taskExecutor.execute后");
		
		Runnable r2 = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				log.info("=============r2");
			}
		};
		Future<?> submit = taskExecutor.submit(r2);
		Object object = submit.get();
		System.out.println("======r2结束:" + object);
		
		Callable<User> call1 = new Callable<User>() {
			@Override
			public User call() throws Exception {
				log.info("=============call1");
				User user = new User();
				user.setId(110);
				user.setName("110-name");
				return user;
			}
		};
		Future<User> submit2 = taskExecutor.submit(call1);
		User user = submit2.get();
		
		log.info("=============call1结果：{}", user);
	}


}
